package com.bdqn.tour.servlet.comments;


import com.bdqn.tour.dao.HotelImplementationClass;
import com.bdqn.tour.dao.impl.ImplUersDao;
import com.bdqn.tour.entity.Comment;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/forum")
public class ForumServlet extends HttpServlet {

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 将评论列表放入请求属性
     this.doPost(request,response);
    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException, ServletException, IOException {
        request.setCharacterEncoding("utf-8");

        // 获取表单数据
        String username = request.getParameter("name");
        String commentContent = request.getParameter("comm"); // 评论内容通过一个名为 "comm" 的参数传递

        // 创建评论对象
        Comment newComment = new Comment();
        newComment.setUsername(username);
        newComment.setUsercomment(commentContent);

        //  HotelImplementationClass 是一个接口，ImplUsersDao 是它的实现类
        HotelImplementationClass userService = new ImplUersDao();

        // 将新的评论添加到数据库
        userService.addComment(newComment);

        // 获取现有的评论列表
        List<Comment> comments = userService.getAllComments();

        // 将评论列表放入请求属性
        request.setAttribute("comments", comments);

        // 转发到 JSP 页面
        request.getRequestDispatcher("").forward(request, response);
    }
}